extends Control

@onready var start_btn: Button = $VBoxContainer/StartButton
@onready var settings_btn: Button = $VBoxContainer/SettingsButton
@onready var quit_btn: Button = $VBoxContainer/QuitButton
@onready var settings_panel: Control = $SettingsPanel

func _ready():
	# 确保连接按钮信号
	print("[MainMenu] _ready")
	if not start_btn.pressed.is_connected(_on_start_button_pressed):
		start_btn.pressed.connect(_on_start_button_pressed)
	if not settings_btn.pressed.is_connected(_on_settings_button_pressed):
		settings_btn.pressed.connect(_on_settings_button_pressed)
	if not quit_btn.pressed.is_connected(_on_quit_button_pressed):
		quit_btn.pressed.connect(_on_quit_button_pressed)
	# 默认隐藏设置面板
	if settings_panel:
		settings_panel.visible = false

func _on_start_button_pressed():
	# 切换到游戏场景
	get_tree().change_scene_to_file("res://scenes/game_scene.tscn")

func _on_settings_button_pressed():
	# 切换设置面板显示/隐藏
	if not settings_panel and has_node("SettingsPanel"):
		settings_panel = $SettingsPanel
	if settings_panel:
		var new_visible := not settings_panel.visible
		settings_panel.visible = new_visible
		settings_panel.mouse_filter = Control.MOUSE_FILTER_STOP
		settings_panel.z_index = 100
		# 确保在最上层绘制
		if settings_panel.get_parent():
			var parent := settings_panel.get_parent()
			parent.move_child(settings_panel, parent.get_child_count() - 1)
		print("[MainMenu] Settings toggled -> ", new_visible)

func _on_quit_button_pressed():
	# 退出游戏wadwd
	get_tree().quit()
